//轮播图模块
$(function () {
    var swiper = new Swiper("#swiper", {
        //点击左右按钮滑动
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        //自动切换图片
        autoplay: {
            delay: 3000,//3秒切换一次
        },
        //分页器
        pagination: {
            el: '.swiper-pagination',
            //点击分页器切换图片
            clickable: true,
        },
        //无缝滚动
        loop: true,
    })

})
//模糊搜素模块
$(function () {
    var search_ipt = $("#search");
    var list_ele = $("#list");
    search_ipt.on("input", function () {
        var options = {
            url: "https://www.baidu.com/sugrec",
            data: {
                prod: "pc",
                from: "pc_web",
                wd: search_ipt.val()
            },
            jsonp: "cb",
            dataType: "jsonp",
            success: function (res) {
                render(res);
            }
        }
        $.ajax(options)
    })
    //失去焦点事件 让ul消失
    search_ipt.on("blur", function () {
        list_ele.css({
            display: "none"
        })

    })
    search_ipt.on("focus", function () {
        list_ele.css({
            backgroundColor: 'white',

        })

    })

    //回调函数 判断获取的数据是否为数组，是则进行渲染
    function render(data) {
        if (data.g instanceof Array) {
            var html = data.g.map(function (item) {
                return `<li class="lili">${item.q}</li>`
            }).join("");
            list_ele.html(html);

        } else {
            list_ele.html();
        }
    }
    list_ele.css({
        width: "400px",
        height: "200px",

    })
    //     $(".lili").css({
    //         fontSize: "12px",
    //         height: "15px",
    //         lineHeight: "15px",
    //         backgroundColor: "red"

    //     })
})
//楼梯模块
$(function () {
    //让楼梯按钮在特定高度下显示
    var $stairs_btn = $(".elevat");
    $(window).scroll(function () {
        //获取scrollTop
        var scrollTop = $("body,html").scrollTop();
        if (scrollTop > 600) {
            $stairs_btn.show();
        } else {
            $stairs_btn.hide();
        }
    });

    //
    //点击让页面跳转到对应的楼层
    var $zq = $(".zq");
    $(".elevat li").click(function () {
        var index = $(this).index(".elevat li ");
        var _top = $zq.eq(index).offset().top;
        $("html,body").stop(true).animate({
            scrollTop: _top
        })
    })
})
//页面渲染
$(function () {
    var options = {
        url: "./static/first.json",
        type: "GET",
        datatype: "json",
        success: function (res) {
            console.log(res);
            render(res);
        },
        error: function (xhr) {
            console.log(xhr);
        }
    }
    $.ajax(options);


})
function render(arr) {
    var group = document.querySelector(".group");
    var active = document.querySelector(".active");
    group.innerHTML = arr.data.map(item =>

        `
             <div class="group-one">
                            <img src="${item.img}" alt="">
                            <p>${item.chan1}</p>
                      </div>
        `
    ).join("");
    active.innerHTML = arr.picture.map(item =>
        `
        <div class="item">
            <div class="item-clo">
                <img src="${item.img}" alt="">
            </div>
            <div class="text-box">
                <h4>${item.title}</h4>
                <span>${item.address}</span>
                <p>${item.con}</p>
            </div>
        </div>
        `).join("");
}
//懒加载模块
$(function () {
    $(".img").lazyload();
});
//二级菜单模块
$(function () {
    $(".header-center li").mouseover(function () {
        $(".menu").show();
    })
    $(".header-center").mouseleave(function () {
        $(".menu").hide();
    })

})
